package com.sali.其他;

import java.util.ArrayList;
import java.util.List;

/**
 和为S的连续正数序列
 */
public class JZ_74 {

    public ArrayList<ArrayList<Integer>> FindContinuousSequence (int sum) {
        // write code here
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        int left = 1;
        int right = 1;
        int tempSum = 0;

        while (right <= sum / 2 + 1) {
            tempSum += right;

            while ( tempSum > sum ) {
                tempSum -= left;
                left++;
            }

            if ( tempSum == sum && right - left + 1 >= 2 ) {
                List<Integer> tmp = new ArrayList<>();
                for ( int i = left; i <= right; i++ ) {
                    tmp.add(i);
                }
                res.add(new ArrayList<>(tmp));
            }

            right++;
        }
        return res;
    }

}
